package thread;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.util.Timer;
import java.util.TimerTask;


public class WatchFiles {
	public static void main(String[] args) {
		TimerTask task = new TimerTask() {
			File[] files=new File[]{new File("C:\\1.txt"),new File("C:\\2.txt"),new File("C:\\3.txt"),new File("C:\\4.txt")};
			public void run() {
				System.out.println("run timertask");
				boolean changed=false;
				for (int i = 0; i < files.length; i++) {
					System.out.println(i+":"+files[i].length());
					if(files[i].length()>0)
						changed=true;
				}
				if(changed){
					new Thread(){
						public void run() {
							System.out.println("run new thread");
							File backup=new File("C:\\backup.txt");
							BufferedReader br=null;
							PrintWriter pw=null;
							for (int i = 0; i < files.length; i++) {
								if (files[i].length()>0) {
									try {
										br=new BufferedReader(new InputStreamReader(new FileInputStream(files[i])));
										pw=new PrintWriter(new BufferedWriter(new OutputStreamWriter(new FileOutputStream(backup, true))));
										String line=null;
										while((line=br.readLine())!=null){
											pw.println(line);
											pw.flush();
										}
										br.close();
										pw.close();
										files[i].delete();
										files[i].createNewFile();
									} catch (IOException e) {
										e.printStackTrace();
									}
								}
							}
						};
					}.start();
				}
			}
		};
		
		
		Timer t = new Timer();
		t.schedule(task, 0, 10000);
		
		
		
		/////////////////////////////////////////
//		Thread watchFiles = new Thread() {
//			public void run() {
//				System.out.println("run watchfiles");
//				Timer t = new Timer();
//				t.schedule(task, 0, 10000);
//			}
//			TimerTask task = new TimerTask() {
//				File[] files=new File[]{new File("C:\\1.txt"),new File("C:\\2.txt"),new File("C:\\3.txt"),new File("C:\\4.txt")};
//				public void run() {
//					System.out.println("run timertask");
//					boolean changed=false;
//					for (int i = 0; i < files.length; i++) {
//						System.out.println(i+":"+files[i].length());
//						if(files[i].length()>0)
//							changed=true;
//					}
//					if(changed){
//						new Thread(){
//							public void run() {
//								System.out.println("run new thread");
//								File backup=new File("C:\\backup.txt");
//								BufferedReader br=null;
//								PrintWriter pw=null;
//								for (int i = 0; i < files.length; i++) {
//									if (files[i].length()>0) {
//										try {
//											br=new BufferedReader(new InputStreamReader(new FileInputStream(files[i])));
//											pw=new PrintWriter(new BufferedWriter(new OutputStreamWriter(new FileOutputStream(backup, true))));
//											String line=null;
//											while((line=br.readLine())!=null){
//												pw.println(line);
//												pw.flush();
//											}
//											br.close();
//											pw.close();
//											files[i].delete();
//											files[i].createNewFile();
//										} catch (IOException e) {
//											e.printStackTrace();
//										}
//									}
//								}
//							};
//						}.start();
//					}
//				}
//			};
//		};
//		watchFiles.start();
	}
}
